package com.example.learn_gl_android.anglib.util.math

import android.opengl.Matrix

data class Matrix4x4(val elements: FloatArray = FloatArray(4*4)) : Cloneable {

    companion object {
        fun one() : Matrix4x4 {
            val ret = Matrix4x4()
            ret.elements[0] = 1f
            ret.elements[5] = 1f
            ret.elements[10] = 1f
            ret.elements[15] = 1f
            return ret
        }
    }

    operator fun plus(other: Matrix4x4) : Matrix4x4{
        val ret = FloatArray(4*4)
        for ( i in 0..15 ){
            ret[i] = elements[i] + other.elements[i]
        }
        return Matrix4x4(ret)
    }

    operator fun plusAssign(other: Matrix4x4){
        for ( i in 0..15 ){
            elements[i] = elements[i] + other.elements[i]
        }
    }

    operator fun minus(other: Matrix4x4) : Matrix4x4{
        val ret = FloatArray(4*4)
        for ( i in 0..15 ){
            ret[i] = elements[i] - other.elements[i]
        }
        return Matrix4x4(ret)
    }

    operator fun minusAssign(other: Matrix4x4){
        for ( i in 0..15 ){
            elements[i] = elements[i] - other.elements[i]
        }
    }

    operator fun times (other : Matrix4x4) : Matrix4x4 {
        val ret = one()
        Matrix.multiplyMM(ret.elements, 0,
                            this.elements, 0,
                            other.elements,  0)
        return ret
    }

    operator fun timesAssign (other : Matrix4x4) {
        val copy = this.elements.clone()

        Matrix.multiplyMM(this.elements, 0,
            copy, 0,
            other.elements,  0)
    }

    override fun clone(): Matrix4x4 {
        return Matrix4x4(this.elements.clone())
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Matrix4x4

        return elements.contentEquals(other.elements)
    }

    override fun hashCode(): Int {
        return elements.contentHashCode()
    }


}